package com.example.springbootdemo09.controller;

import com.example.springbootdemo09.domain.Teacher;
import com.example.springbootdemo09.query.TeacherQuery;
import com.example.springbootdemo09.service.ITeacherService;
import com.example.springbootdemo09.service.impl.TeacherServiceImpl;
import com.example.springbootdemo09.utils.ExcelUtil;
import com.example.springbootdemo09.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService igs;
    //查询所有

    /**
     * 查询
     * @return
     */
    @GetMapping
    R findAll(){
        List<Teacher> all = igs.findAll();
        return R.ok(all);

    }
    //查询单个
    /**
     * 查询单个
     * @return
     */
    @GetMapping("{id}")
    R findOne(@PathVariable("id") Long id){
        Teacher one = igs.findOne(id);
        return R.ok(one);
    }
    //修改
    /**
     * 修改
     * @return
     */
    @PutMapping
    R updete(@RequestBody Teacher teacher){
        try {
            if (teacher.getId()!=null){
                igs.updete(teacher);
            }else {
                igs.add(teacher);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return  R.fail();
        }

    }
    //删除
    /**
     * 删除
     * @return
     */
    @DeleteMapping("{id}")
    R delete(@PathVariable("id") Long id){
        try{
            igs.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }

    }
    /**
     * 批量删除
     * @return
     */
@PatchMapping
    //批量删除
    R PathDelete(@RequestBody Long[]id){
    igs.pathDele(id);
    return  R.ok();
    }
    //APS-gsoRcWKwhPcLsjXhe2YNJ0jxsw8vHCVZ
    //3976915

    @PostMapping
    //高级分页查询
    R QueryFenye(@RequestBody TeacherQuery teacherQuery){
        R fenye = igs.fenye(teacherQuery);
        return  R.ok(fenye);
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Teacher> list = igs.findAll();
        ExcelUtil.exportExcel(list, "智慧校园管理系统-老师信息", "老师信息表", Teacher.class, "Teachers", response);

    }
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        List<Teacher> list = new ArrayList<>();
        ExcelUtil.exportExcel(list,"老师信息","老师",Teacher.class,"teacher",response);
    }
    @PostMapping("/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        // 1.使用工具类从复杂文件中获取到老师信息集合
        List<Teacher> teachers = ExcelUtil.importExcel(file, Teacher.class);
        // 2.调用批量新增方法，保存所有老师信息
        //teacherService.patchAdd(teachers);
        System.out.println("导入的数据如下...");
        teachers.forEach(System.out::println);
        //导出不能返回R，因为导出用了response响应，R也是响应，两个响应冲突了，前端接收哪一个响应？
        //导入中没有使用过response，只返回R，不会冲突
        return R.ok();
    }
}
